//封装axios
import axios from "axios";

import cookie from 'react-cookies'

const request = axios.create({
    // baseURL: 'https://apipc-xiaotuxian-front.itheima.net',
    baseURL: 'https://pcapi-xiaotuxian-front-devtest.itheima.net',
    // timeout: 5000,
})
// https://pcapi-xiaotuxian-front-devtest.itheima.net
// https://apipc-xiaotuxian-front.itheima.net
// 请求拦截器
request.interceptors.request.use((config) => {

    // 从 cookie 中获取 userInfo  
    const userInfo = cookie.load('userInfo');

    // 如果 userInfo 存在且含有 token，则添加到请求 headers 中  
    if (userInfo && userInfo.token) {
        config.headers['Authorization'] = `Bearer ${userInfo.token}`;
    }


    return config
})

//响应拦截器
request.interceptors.response.use((response) => {
    return response.data
}, (error) => {
    return Promise.reject(new Error(error.message))
})


export default request